﻿using System;
using System.Collections.Generic;
using System.Text;
using SocialExplorer.IO.FastDBF;
using System.IO;
using System.Diagnostics;
using System.Data;

namespace Soil
{
    /// <summary>
    /// A polygon in the soil map
    /// </summary>
    class SoilPolygon
    {
        public SoilPolygon(DbfRecord polygonRecord)
        {
            _id = polygonRecord["POLY_ID"];
            _area_ha = double.Parse(polygonRecord["HECTARES"]);
        }

        public SoilPolygon(DotSpatial.Data.Feature fea)
        {
            _id = fea.DataRow["POLY_ID"].ToString();

            bool hasHectaresCol = fea.DataRow.Table.Columns.IndexOf("HECTARES") > -1;
            if(hasHectaresCol)
                _area_ha = double.Parse(fea.DataRow["HECTARES"].ToString());

            double realArea = 0.0;
            DotSpatial.Topology.Polygon p = fea.BasicGeometry as DotSpatial.Topology.Polygon;
            if (p == null)
            {
                DotSpatial.Topology.MultiPolygon pp = fea.BasicGeometry as DotSpatial.Topology.MultiPolygon;
                if (pp != null) realArea = pp.Area;
                else
                    System.Diagnostics.Debug.WriteLine(string.Format("Unknow shape {0}", _id));
            }
            else
                realArea = p.Area;

            realArea /= 10000.0;
            if (hasHectaresCol && _area_ha != realArea)            
                System.Diagnostics.Debug.WriteLine(string.Format("Polygon Area Difference for {2}: {0} vs. {1}",_area_ha,realArea,_id));
            
            _area_ha = realArea;
        }

        private string _id = "";
        private double _area_ha = 0.0;
        private List<SoilComponent> _cmps = new List<SoilComponent>();

        public string ID { get { return _id; } }
        public double Area { get { return _area_ha; } }
        public List<SoilComponent> Components
        {
            get { return _cmps; }
            set { _cmps = value; }
        }
        public override string ToString()
        {
            string s = _id;
            foreach (SoilComponent cmp in _cmps)
                s += "_" + cmp.Percent.ToString();

            return s;
        }

        /// <summary>
        /// The dominent component
        /// </summary>
        public SoilComponent DominentComponent
        {
            get
            {
                if (_cmps.Count == 0) return null;
                return _cmps[_cmps.Count - 1]; //assume it's already sort ase
            }
        }

        SoilComponent _mergedCmp = null;

        /// <summary>
        /// The single component for the polygon. 
        /// </summary>
        /// <remarks>It could be one original component or a new coponent merged from several components.</remarks>
        public SoilComponent MergedComponent
        {
            get
            {
                if (_mergedCmp == null)
                {
                    if (_cmps.Count == 0) return null;
                    if (_cmps.Count == 1) return _cmps[0];

                    foreach (SoilComponent cmp in _cmps)
                    {
                        if (_mergedCmp == null) _mergedCmp = cmp;
                        else _mergedCmp = _mergedCmp.Merge(cmp);
                    }

                    //check if the polygon is completed
                    if (_mergedCmp.Percent < 1)
                        Debug.WriteLine("The polygon " + _id + " is not completed.");
                }
                return _mergedCmp;
            }
        }
    }
}
